﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.AlbLab.GEP.Dao.Interface;
using Com.AlbLab.GEP.Dao.Base;
using Oracle.DataAccess.Client;
using Com.AlbLab.GEP.Domain.Entities;
using System.Data;
using Com.AlbLab.GEP.Domain;

namespace Com.AlbLab.GEP.Dao.Impl
{
    public class DocumentoDao : OracleDao<OracleSessionManager, ISessionModel>, IDocumentoDao
    {
        public DocumentoDao(OracleSessionManager daoContext)
            : base(daoContext)
        {
            if (!base.Context.Session_isActive())
            {
                base.Context.Session_Init();
                base.CurrentConnection.Open();
            }
        }

        private string selectById = "select * from GEP_BLOB WHERE BLB_ID_FASE=: BLB_ID_FASE";
        private string delete = "delete from GEP_BLOB WHERE BLB_ID_FASE=: BLB_ID_FASE";
        private string insert = "INSERT INTO GEP_BLOB (BLB_ID_FASE,BLB_INTESTAZIONE_DOC,BLB_DOC)"
                              + " VALUES (:BLB_ID_FASE, :BLB_INTESTAZIONE_DOC, :BLB_DOC)";

        private OracleParameter[] MappingParams(GEP_FASE.GEP_BLOB r)
        {
            OracleParameter[] oparams;
            oparams = new OracleParameter[3];

            oparams[0] = new OracleParameter("BLB_ID_FASE", OracleDbType.Decimal, r.Blb_Id, ParameterDirection.Input);
            oparams[1] = new OracleParameter("BLB_INTESTAZIONE_DOC", OracleDbType.Varchar2, r.Blb_intestazione_doc, ParameterDirection.Input);
            oparams[2] = new OracleParameter("BLB_DOC", OracleDbType.Blob, r.Blb_doc, ParameterDirection.Input);
           
            return oparams;
        }

        #region IDao<GEP_BLOB,decimal> Members

        public Domain.Entities.GEP_FASE.GEP_BLOB GetById(decimal id)
        {
            Oracle.DataAccess.Client.OracleCommand cmd = new Oracle.DataAccess.Client.OracleCommand(selectById, CurrentConnection);
            cmd.Parameters.Add("BLB_ID_FASE", id);
            Domain.Entities.GEP_FASE.GEP_BLOB ut = null;
            try
            {
                using (OracleDataReader r = cmd.ExecuteReader())
                {
                    if (r.HasRows)
                        r.Read();
                    ut = DaoOracleDbHelper.MapDocumento(r);
                }
            }
            catch (Exception e0x)
            {
                throw new ManagedException("EX-FD002", e0x);
            }
            return ut;
        }

        public void Insert(Domain.Entities.GEP_FASE.GEP_BLOB entity)
        {
            using (OracleCommand ocmd = new OracleCommand())
            {
                try
                {
                    ocmd.CommandText = insert;
                    ocmd.Parameters.AddRange(MappingParams(entity));
                    ocmd.Connection = base.CurrentConnection;
                    ocmd.ExecuteNonQuery();
                }
                catch (Exception e0x)
                {
                    throw new ManagedException("EX-FD005", e0x);
                }
            }
        }

        public void Delete(decimal id)
        {
            using (OracleCommand ocmd = new OracleCommand())
            {
                try
                {
                    ocmd.CommandText = delete;
                    ocmd.Parameters.Add("BLB_ID_FASE", id);
                    ocmd.Connection = base.CurrentConnection;
                    ocmd.ExecuteNonQuery();
                }
                catch (Exception e0x)
                {
                    throw new ManagedException("EX-FS005", e0x);
                }
            }
        }

        public Dictionary<decimal, Domain.Entities.GEP_FASE.GEP_BLOB> GetAll()
        {
            throw new NotImplementedException();
        }

        public void Update(Domain.Entities.GEP_FASE.GEP_BLOB entity)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
